//
//  YYCoreDataHandler.m
//  SQLAndCoreData
//
//  Created by lanou on 16/3/29.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "YYCoreDataHandlerRecord.h"
#import "AppDelegate.h"

@implementation YYCoreDataHandlerRecord

static YYCoreDataHandlerRecord *sharedCoreDataHandler = nil;

+ (YYCoreDataHandlerRecord *)sharedCoreDataHandler {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedCoreDataHandler = [[YYCoreDataHandlerRecord alloc] init];
    });
    return sharedCoreDataHandler;
}

+ (kYYCoreDataObject *)getNewManagedObject {
    return [[kYYCoreDataObject alloc] initWithEntity:[NSEntityDescription entityForName:kYYCoreDataObjectString inManagedObjectContext:[self sharedCoreDataHandler].managedObjectContext] insertIntoManagedObjectContext:nil];
}

+ (kYYCoreDataObject *)getNewManagedObjectAndInsertIntoManagedObjectContext {
    return [NSEntityDescription insertNewObjectForEntityForName:kYYCoreDataObjectString inManagedObjectContext:[self sharedCoreDataHandler].managedObjectContext];
}

- (BOOL)insertObject:(kYYCoreDataObject *)aObject {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kYYCoreDataObjectString];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", kYYCoreDataObjectKeyDescription, aObject.kYYCoreDataObjectKeyProperty];
    fetchRequest.predicate = predicate;
    NSError *error = nil;
    [context executeFetchRequest:fetchRequest error:&error];
    if ([context countForFetchRequest:fetchRequest error:nil] > 0) {
        return NO;
    }
    [context insertObject:aObject];
    [self saveContext];
    return YES;
}

- (BOOL)deleteObjectByKey:(kYYCoreDataObjectKey *)aKey {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kYYCoreDataObjectString];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", kYYCoreDataObjectKeyDescription, aKey];
    fetchRequest.predicate = predicate;
    NSArray *objects = [context executeFetchRequest:fetchRequest error:nil];
    if ([context countForFetchRequest:fetchRequest error:nil] == 0) {
        return NO;
    }
    [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [context deleteObject:obj];
    }];
    [self saveContext];
    return YES;
}

- (BOOL)updateObjectByKey:(kYYCoreDataObjectKey *)aKey withDictionary:(NSDictionary *)aDict {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kYYCoreDataObjectString];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", kYYCoreDataObjectKeyDescription, aKey];
    fetchRequest.predicate = predicate;
    NSArray *objects = [context executeFetchRequest:fetchRequest error:nil];
    if ([context countForFetchRequest:fetchRequest error:nil] == 0) {
        return NO;
    }
    [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        kYYCoreDataObject *object = obj;
        [obj setValuesForKeysWithDictionary:aDict];
        
    }];
    [self saveContext];
    return YES;
}

- (NSArray<kYYCoreDataObject *> *)selectAllObjects {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kYYCoreDataObjectString];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:kYYCoreDataObjectSortDescriptorString ascending:YES];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    // 4.执行查询请求
    NSError *error = nil;
    NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
    if (objects == nil) {
        NSLog(@"%@", [error localizedDescription]);
    }
    return objects;
}

- (NSArray<kYYCoreDataObject *> *)selectObjectByKey:(kYYCoreDataObjectKey *)aKey {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entittDescription = [NSEntityDescription entityForName:kYYCoreDataObjectString inManagedObjectContext:context];
    fetchRequest.entity = entittDescription;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category = %@", aKey];
    fetchRequest.predicate = predicate;
    NSError *error = nil;
    NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
    if (objects == nil) {
        NSLog(@"%@", [error localizedDescription]);
    }
    return objects;
}

- (AppDelegate *)applicationDelegate {
    return [UIApplication sharedApplication].delegate;
}

- (NSManagedObjectContext *)managedObjectContext {
    return [self applicationDelegate].managedObjectContext;
}

- (void)saveContext {
    [[self applicationDelegate] saveContext];
}

@end
